from django.views.generic.base import View
from apps.goods.models import Goods
from django.http import HttpResponse
import json


class GoodsListView(View):

    def get(self, request):
        """
        通过django的view实现商品列表页
        :param request:
        :return:
        """
        json_list = []
        goods = Goods.objects.all()
        # for good in goods:
        #     json_dict = {'id': good.category.id, 'name': good.name, 'category': good.category.name,
        #                  "market_price": good.market_price}
        #     json_list.append(json_dict)
        # from django.forms.models import model_to_dict
        # for good in goods:
        #     json_dict = model_to_dict(good)
        #     json_list.append(json_dict)
        from django.core import serializers
        json_data = serializers.serialize('json', goods)
        json_data = json.loads(json_data)
        from django.http import StreamingHttpResponse, JsonResponse
        return JsonResponse(json_data, safe=False)
